<template>
  <div class="app-container">
    <el-input v-model="filterText"  placeholder="Filter keyword" style="margin-bottom:30px;"
    />
    <el-tree
      ref="subjectTree"
      :data="subjectList"
      :props="defaultProps"
      :filter-node-method="filterNode"
      class="filter-tree"
      default-expand-all
    />
  </div>
</template>
<script>
import subject from '@/api/edu/subject'

export default {
  data() {
    return {
      filterText: '',
      subjectList: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      }
    }
  },
  watch: {
    filterText(val) {
      // val就是input输入的内容
      // console.log(val)
      this.$refs.subjectTree.filter(val)
    }
  },

  created() {
    // 得到课程列表
    this.fetchNodeList()
  },
  methods: {
    fetchNodeList() {
      subject.getSubjectList()
        .then(response => {
          if (response.success === true) {
            this.subjectList = response.data.list
            // console.log(this.subjectList)

          }
        })
    },

    // 搜索功能
    filterNode(value, data) {
      if (!value) return true
      // 转换为小写进行匹配
      return data.title.toLowerCase().indexOf(value.toLowerCase()) !== -1
    }
  }
}
</script>

<style scoped>

</style>
